package com.example.batbat.view;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.batbat.R;
import com.example.batbat.viewmodel.SplashViewModel;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {
    private SplashViewModel viewModel;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //限制界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //实例化viewModel
        viewModel= new ViewModelProvider(this,new ViewModelProvider
                .AndroidViewModelFactory(getApplication()))
                .get(SplashViewModel.class);

        //初始化完成后跳转
        viewModel.getIsLoadDone().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if(1==integer){
                    viewModel.getAvatar();
                }
                if(2==integer){
                    viewModel.setFavorMap();
                }
                if(3==integer){
                    //发送intent实现页面跳转，第一个参数为当前页面的context，第二个参数为要跳转的主页
                    Intent intent = new Intent(SplashActivity.this, MainActivity2.class);
                    startActivity(intent);
                    SplashActivity.this.finish();
                }
            }
        });

        //数据加载
        init();
    }
    private void init() {

        //判断是否登录，或token是否过期
        viewModel.isLogin();
//        //初始化头像
//        viewModel.getAvatar();
//        //初始化用户喜爱游戏
//        viewModel.setFavorMap();
    }
}
